package com.travelsmart.server.control;

import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.travelsmart.client.bean.to.FilterInfoHotel;
import com.travelsmart.client.bean.to.FilterPesquisaHotel;
import com.travelsmart.client.bean.to.HotelTO;
import com.travelsmart.client.bean.to.ReservaHotelException;
import com.travelsmart.client.bean.to.ReservaHotelTO;
import com.travelsmart.client.control.HotelSmartService;
import com.travelsmart.server.integration.HotelSmartClient;
import com.travelsmart.server.util.BasicComparator;
import com.travelsmart.server.util.SortedHeader;

@SuppressWarnings("serial")
public class HotelSmartServiceImpl extends RemoteServiceServlet implements
		HotelSmartService {


	@Override
	public List<HotelTO> listaHotel(FilterPesquisaHotel filter) {
		try {
			return HotelSmartClient.getInstance().pesquisarHoteisDisponiveis(
					filter);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public HotelTO dadosHotel(FilterInfoHotel filter) {
		HotelTO to;
		try {
			to = HotelSmartClient.getInstance().infoHotel(filter);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return to;
	}

	public <E> List<E> sortList(List<E> list, boolean asc, String atributo) {
		SortedHeader<E> sorter = new SortedHeader<E>(list);
		sorter.setSortAscending(asc);
		sorter.setSortColumn(atributo);
		sorter.setSortAscendingOld(asc);
		sorter.setSortColumnOld("");
		sorter.setCustomComparator(new BasicComparator<E>(asc, atributo));
		return sorter.getValues();
	}

	@Override
	public void updateReserva(ReservaHotelTO reserva) {
		// TODO Auto-generated method stub
		// HotelSmartClient.getInstance().bookHotel(reserva);

	}

	@Override
	public ReservaHotelTO criarReservaHotel(ReservaHotelTO to)
			throws ReservaHotelException {
		try {
			HotelSmartClient.getInstance().bookHotel(to,
					this.getThreadLocalRequest().getUserPrincipal().getName());
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReservaHotelException(e.getMessage());
		}
		return to;
	}

	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/

	
		public ReservaHotelTO obterReservaHotel(long id) throws ReservaHotelException
				
		{
			ReservaHotelTO reserva = null;
			
			try {
				reserva = HotelSmartClient.getInstance().getInfoBookHotel(id);
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new ReservaHotelException(e.getMessage());
			}
			return reserva;
		}
		
		
		public List<ReservaHotelTO> listReservas (int posicaoInicial, int nroRegistrosPagina) throws Exception {
			List<ReservaHotelTO> list = null;
			try{
				list = HotelSmartClient.getInstance().listReserva(this.getThreadLocalRequest().getUserPrincipal().getName(), nroRegistrosPagina, posicaoInicial);
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
			return list;
	
			
		}
}


